#pragma once
#include "RobotApp.h"

#pragma managed

namespace RobotEmulator {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Threading;
	using namespace System::Text;
	using namespace System::IO;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//

			this->waitHandle = gcnew EventWaitHandle(false,EventResetMode::AutoReset);
			robotApp = gcnew RobotApp();

			textBoxAppName->Text = gcnew String(robotApp->GetAppName());
			robotApp->Initialize();

			this->simulatorBaseControlUnit->RobotBase = robotApp;
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: SimulatorBase::SimulatorBaseControl^  simulatorBaseControlUnit;
	protected: 

	protected: 

	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Panel^  panel1;
	private: System::Windows::Forms::Button^  buttonOnOff;

	private: System::Windows::Forms::Button^  buttonProgram;
	private: System::Windows::Forms::Button^  buttonView;
	private: System::Windows::Forms::TextBox^  textBoxRCXLCD;
	private: System::Windows::Forms::Button^  buttonRun;
	private: System::Windows::Forms::TextBox^  textBoxRobotOrder;
	private: System::Windows::Forms::Button^  buttonStart;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  textBoxAppName;
	private: System::Windows::Forms::Label^  label3;

	private: System::Windows::Forms::Label^  label6;
	private: System::Windows::Forms::Label^  label7;
	private: System::Windows::Forms::TextBox^  textBoxBatteryLevel;
	private: System::Windows::Forms::TextBox^  textBoxWheelMotorDir;
	private: System::Windows::Forms::TextBox^  textBoxSteeringMotorDir;



	private: System::Windows::Forms::CheckBox^  checkBox1;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::TextBox^  textBoxSound;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::ListBox^  listBoxSensors;
	private: System::Windows::Forms::TextBox^  textBoxLightSensorValue;
	private: System::Windows::Forms::Button^  buttonAddLightSensorValue;
	private: System::Windows::Forms::Button^  buttonClearLightSensorValue;
	private: System::Windows::Forms::CheckBox^  checkBoxStepExecute;
	private: System::Windows::Forms::Button^  buttonExecuteContinue;
	private: System::Windows::Forms::Label^  label8;
	private: System::Windows::Forms::Label^  label9;
	private: System::Windows::Forms::TextBox^  textBoxSoundDuration;
	private: System::Windows::Forms::TextBox^  textBoxSoundInternote;
	private: System::Windows::Forms::Label^  label10;
	private: System::Windows::Forms::TextBox^  textBoxLightSensorBrightness;
	private: System::Windows::Forms::TextBox^  textBoxWheelMotorSpeed;
	private: System::Windows::Forms::TextBox^  textBoxSteeringMotorSpeed;
	private: System::Windows::Forms::Button^  buttonBattery;




	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			SimulatorBase::RobotSimCon^  robotSimCon1 = (gcnew SimulatorBase::RobotSimCon());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->panel1 = (gcnew System::Windows::Forms::Panel());
			this->buttonRun = (gcnew System::Windows::Forms::Button());
			this->buttonOnOff = (gcnew System::Windows::Forms::Button());
			this->buttonProgram = (gcnew System::Windows::Forms::Button());
			this->buttonView = (gcnew System::Windows::Forms::Button());
			this->textBoxRCXLCD = (gcnew System::Windows::Forms::TextBox());
			this->textBoxRobotOrder = (gcnew System::Windows::Forms::TextBox());
			this->buttonStart = (gcnew System::Windows::Forms::Button());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->textBoxAppName = (gcnew System::Windows::Forms::TextBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->label7 = (gcnew System::Windows::Forms::Label());
			this->textBoxBatteryLevel = (gcnew System::Windows::Forms::TextBox());
			this->textBoxWheelMotorDir = (gcnew System::Windows::Forms::TextBox());
			this->textBoxSteeringMotorDir = (gcnew System::Windows::Forms::TextBox());
			this->checkBox1 = (gcnew System::Windows::Forms::CheckBox());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->textBoxSound = (gcnew System::Windows::Forms::TextBox());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->listBoxSensors = (gcnew System::Windows::Forms::ListBox());
			this->textBoxLightSensorValue = (gcnew System::Windows::Forms::TextBox());
			this->buttonAddLightSensorValue = (gcnew System::Windows::Forms::Button());
			this->buttonClearLightSensorValue = (gcnew System::Windows::Forms::Button());
			this->checkBoxStepExecute = (gcnew System::Windows::Forms::CheckBox());
			this->buttonExecuteContinue = (gcnew System::Windows::Forms::Button());
			this->simulatorBaseControlUnit = (gcnew SimulatorBase::SimulatorBaseControl());
			this->label8 = (gcnew System::Windows::Forms::Label());
			this->label9 = (gcnew System::Windows::Forms::Label());
			this->textBoxSoundDuration = (gcnew System::Windows::Forms::TextBox());
			this->textBoxSoundInternote = (gcnew System::Windows::Forms::TextBox());
			this->label10 = (gcnew System::Windows::Forms::Label());
			this->textBoxLightSensorBrightness = (gcnew System::Windows::Forms::TextBox());
			this->textBoxWheelMotorSpeed = (gcnew System::Windows::Forms::TextBox());
			this->textBoxSteeringMotorSpeed = (gcnew System::Windows::Forms::TextBox());
			this->buttonBattery = (gcnew System::Windows::Forms::Button());
			this->panel1->SuspendLayout();
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label1->Location = System::Drawing::Point(247, 48);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(169, 16);
			this->label1->TabIndex = 2;
			this->label1->Text = L"Control Order Result";
			// 
			// panel1
			// 
			this->panel1->BackColor = System::Drawing::Color::Yellow;
			this->panel1->Controls->Add(this->buttonRun);
			this->panel1->Controls->Add(this->buttonOnOff);
			this->panel1->Controls->Add(this->buttonProgram);
			this->panel1->Controls->Add(this->buttonView);
			this->panel1->Controls->Add(this->textBoxRCXLCD);
			this->panel1->Location = System::Drawing::Point(12, 263);
			this->panel1->Name = L"panel1";
			this->panel1->Size = System::Drawing::Size(188, 100);
			this->panel1->TabIndex = 3;
			// 
			// buttonRun
			// 
			this->buttonRun->Location = System::Drawing::Point(128, 74);
			this->buttonRun->Name = L"buttonRun";
			this->buttonRun->Size = System::Drawing::Size(57, 23);
			this->buttonRun->TabIndex = 4;
			this->buttonRun->Text = L"Run";
			this->buttonRun->UseVisualStyleBackColor = true;
			this->buttonRun->Click += gcnew System::EventHandler(this, &Form1::buttonRun_Click);
			// 
			// buttonOnOff
			// 
			this->buttonOnOff->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->buttonOnOff->Location = System::Drawing::Point(3, 74);
			this->buttonOnOff->Name = L"buttonOnOff";
			this->buttonOnOff->Size = System::Drawing::Size(57, 23);
			this->buttonOnOff->TabIndex = 3;
			this->buttonOnOff->Text = L"On-Off";
			this->buttonOnOff->UseVisualStyleBackColor = true;
			this->buttonOnOff->Click += gcnew System::EventHandler(this, &Form1::buttonOnOff_Click);
			// 
			// buttonProgram
			// 
			this->buttonProgram->Location = System::Drawing::Point(128, 3);
			this->buttonProgram->Name = L"buttonProgram";
			this->buttonProgram->Size = System::Drawing::Size(57, 23);
			this->buttonProgram->TabIndex = 2;
			this->buttonProgram->Text = L"Prgm";
			this->buttonProgram->UseVisualStyleBackColor = true;
			this->buttonProgram->Click += gcnew System::EventHandler(this, &Form1::buttonProgram_Click);
			// 
			// buttonView
			// 
			this->buttonView->Location = System::Drawing::Point(3, 3);
			this->buttonView->Name = L"buttonView";
			this->buttonView->Size = System::Drawing::Size(57, 23);
			this->buttonView->TabIndex = 1;
			this->buttonView->Text = L"View";
			this->buttonView->UseVisualStyleBackColor = true;
			this->buttonView->Click += gcnew System::EventHandler(this, &Form1::buttonView_Click);
			// 
			// textBoxRCXLCD
			// 
			this->textBoxRCXLCD->BackColor = System::Drawing::Color::Silver;
			this->textBoxRCXLCD->Location = System::Drawing::Point(23, 41);
			this->textBoxRCXLCD->Name = L"textBoxRCXLCD";
			this->textBoxRCXLCD->Size = System::Drawing::Size(146, 19);
			this->textBoxRCXLCD->TabIndex = 0;
			// 
			// textBoxRobotOrder
			// 
			this->textBoxRobotOrder->Location = System::Drawing::Point(250, 67);
			this->textBoxRobotOrder->Multiline = true;
			this->textBoxRobotOrder->Name = L"textBoxRobotOrder";
			this->textBoxRobotOrder->ScrollBars = System::Windows::Forms::ScrollBars::Both;
			this->textBoxRobotOrder->Size = System::Drawing::Size(309, 222);
			this->textBoxRobotOrder->TabIndex = 4;
			// 
			// buttonStart
			// 
			this->buttonStart->Location = System::Drawing::Point(250, 21);
			this->buttonStart->Name = L"buttonStart";
			this->buttonStart->Size = System::Drawing::Size(309, 23);
			this->buttonStart->TabIndex = 5;
			this->buttonStart->Text = L"Start";
			this->buttonStart->UseVisualStyleBackColor = true;
			this->buttonStart->Click += gcnew System::EventHandler(this, &Form1::buttonStart_Click);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label2->Location = System::Drawing::Point(17, 22);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(140, 16);
			this->label2->TabIndex = 6;
			this->label2->Text = L"Application Name";
			// 
			// textBoxAppName
			// 
			this->textBoxAppName->Enabled = false;
			this->textBoxAppName->Location = System::Drawing::Point(20, 42);
			this->textBoxAppName->Name = L"textBoxAppName";
			this->textBoxAppName->Size = System::Drawing::Size(177, 19);
			this->textBoxAppName->TabIndex = 7;
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label3->Location = System::Drawing::Point(9, 366);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(71, 16);
			this->label3->TabIndex = 8;
			this->label3->Text = L"Battery:";
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 11.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label6->Location = System::Drawing::Point(9, 402);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(54, 15);
			this->label6->TabIndex = 11;
			this->label6->Text = L"Wheel:";
			// 
			// label7
			// 
			this->label7->AutoSize = true;
			this->label7->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label7->Location = System::Drawing::Point(9, 422);
			this->label7->Name = L"label7";
			this->label7->Size = System::Drawing::Size(76, 16);
			this->label7->TabIndex = 12;
			this->label7->Text = L"Steering:";
			// 
			// textBoxBatteryLevel
			// 
			this->textBoxBatteryLevel->Location = System::Drawing::Point(86, 367);
			this->textBoxBatteryLevel->Name = L"textBoxBatteryLevel";
			this->textBoxBatteryLevel->Size = System::Drawing::Size(77, 19);
			this->textBoxBatteryLevel->TabIndex = 13;
			this->textBoxBatteryLevel->Text = L"0";
			this->textBoxBatteryLevel->TextChanged += gcnew System::EventHandler(this, &Form1::textBoxBatteryLevel_TextChanged);
			// 
			// textBoxWheelMotorDir
			// 
			this->textBoxWheelMotorDir->Enabled = false;
			this->textBoxWheelMotorDir->Location = System::Drawing::Point(91, 398);
			this->textBoxWheelMotorDir->Name = L"textBoxWheelMotorDir";
			this->textBoxWheelMotorDir->Size = System::Drawing::Size(54, 19);
			this->textBoxWheelMotorDir->TabIndex = 15;
			// 
			// textBoxSteeringMotorDir
			// 
			this->textBoxSteeringMotorDir->Enabled = false;
			this->textBoxSteeringMotorDir->Location = System::Drawing::Point(91, 423);
			this->textBoxSteeringMotorDir->Name = L"textBoxSteeringMotorDir";
			this->textBoxSteeringMotorDir->Size = System::Drawing::Size(54, 19);
			this->textBoxSteeringMotorDir->TabIndex = 16;
			// 
			// checkBox1
			// 
			this->checkBox1->AutoSize = true;
			this->checkBox1->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->checkBox1->Location = System::Drawing::Point(250, 490);
			this->checkBox1->Name = L"checkBox1";
			this->checkBox1->Size = System::Drawing::Size(132, 20);
			this->checkBox1->TabIndex = 17;
			this->checkBox1->Text = L"Touch Sensor";
			this->checkBox1->UseVisualStyleBackColor = true;
			this->checkBox1->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox1_CheckedChanged);
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label5->Location = System::Drawing::Point(9, 447);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(59, 16);
			this->label5->TabIndex = 18;
			this->label5->Text = L"Sound:";
			// 
			// textBoxSound
			// 
			this->textBoxSound->Enabled = false;
			this->textBoxSound->Location = System::Drawing::Point(78, 448);
			this->textBoxSound->Name = L"textBoxSound";
			this->textBoxSound->Size = System::Drawing::Size(166, 19);
			this->textBoxSound->TabIndex = 19;
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label4->Location = System::Drawing::Point(247, 346);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(109, 16);
			this->label4->TabIndex = 9;
			this->label4->Text = L"Light Sensor:";
			// 
			// listBoxSensors
			// 
			this->listBoxSensors->FormattingEnabled = true;
			this->listBoxSensors->ItemHeight = 12;
			this->listBoxSensors->Items->AddRange(gcnew cli::array< System::Object^  >(1) {L"0"});
			this->listBoxSensors->Location = System::Drawing::Point(250, 372);
			this->listBoxSensors->Name = L"listBoxSensors";
			this->listBoxSensors->Size = System::Drawing::Size(309, 76);
			this->listBoxSensors->TabIndex = 20;
			// 
			// textBoxLightSensorValue
			// 
			this->textBoxLightSensorValue->Location = System::Drawing::Point(409, 347);
			this->textBoxLightSensorValue->Name = L"textBoxLightSensorValue";
			this->textBoxLightSensorValue->Size = System::Drawing::Size(100, 19);
			this->textBoxLightSensorValue->TabIndex = 21;
			// 
			// buttonAddLightSensorValue
			// 
			this->buttonAddLightSensorValue->Location = System::Drawing::Point(358, 345);
			this->buttonAddLightSensorValue->Name = L"buttonAddLightSensorValue";
			this->buttonAddLightSensorValue->Size = System::Drawing::Size(45, 23);
			this->buttonAddLightSensorValue->TabIndex = 22;
			this->buttonAddLightSensorValue->Text = L"Add";
			this->buttonAddLightSensorValue->UseVisualStyleBackColor = true;
			this->buttonAddLightSensorValue->Click += gcnew System::EventHandler(this, &Form1::buttonAddLightSensorValue_Click);
			// 
			// buttonClearLightSensorValue
			// 
			this->buttonClearLightSensorValue->Location = System::Drawing::Point(515, 345);
			this->buttonClearLightSensorValue->Name = L"buttonClearLightSensorValue";
			this->buttonClearLightSensorValue->Size = System::Drawing::Size(44, 23);
			this->buttonClearLightSensorValue->TabIndex = 23;
			this->buttonClearLightSensorValue->Text = L"Clear";
			this->buttonClearLightSensorValue->UseVisualStyleBackColor = true;
			this->buttonClearLightSensorValue->Click += gcnew System::EventHandler(this, &Form1::buttonClearLightSensorValue_Click);
			// 
			// checkBoxStepExecute
			// 
			this->checkBoxStepExecute->AutoSize = true;
			this->checkBoxStepExecute->Location = System::Drawing::Point(250, 295);
			this->checkBoxStepExecute->Name = L"checkBoxStepExecute";
			this->checkBoxStepExecute->Size = System::Drawing::Size(92, 16);
			this->checkBoxStepExecute->TabIndex = 24;
			this->checkBoxStepExecute->Text = L"Step Execute";
			this->checkBoxStepExecute->UseVisualStyleBackColor = true;
			this->checkBoxStepExecute->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBoxStepExecute_CheckedChanged);
			// 
			// buttonExecuteContinue
			// 
			this->buttonExecuteContinue->Enabled = false;
			this->buttonExecuteContinue->Location = System::Drawing::Point(358, 291);
			this->buttonExecuteContinue->Name = L"buttonExecuteContinue";
			this->buttonExecuteContinue->Size = System::Drawing::Size(75, 23);
			this->buttonExecuteContinue->TabIndex = 25;
			this->buttonExecuteContinue->Text = L"Continue";
			this->buttonExecuteContinue->UseVisualStyleBackColor = true;
			this->buttonExecuteContinue->Click += gcnew System::EventHandler(this, &Form1::buttonExecuteContinue_Click);
			// 
			// simulatorBaseControlUnit
			// 
			this->simulatorBaseControlUnit->BatteryLevel = -1;
			this->simulatorBaseControlUnit->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
			this->simulatorBaseControlUnit->Location = System::Drawing::Point(12, 67);
			this->simulatorBaseControlUnit->Name = L"simulatorBaseControlUnit";
			this->simulatorBaseControlUnit->RobotBase = robotSimCon1;
			this->simulatorBaseControlUnit->SensorS1 = -1;
			this->simulatorBaseControlUnit->SensorS2 = -1;
			this->simulatorBaseControlUnit->SensorS3 = -1;
			this->simulatorBaseControlUnit->Size = System::Drawing::Size(188, 190);
			this->simulatorBaseControlUnit->TabIndex = 0;
			this->simulatorBaseControlUnit->OnClearLCD += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnClearLCD);
			this->simulatorBaseControlUnit->OnSetSoundDuration += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnSetSoundDuration);
			this->simulatorBaseControlUnit->OnSetSoundInternote += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnSetSoundInternote);
			this->simulatorBaseControlUnit->OnNotifyLoopCount += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnNotifyLoopCount);
			this->simulatorBaseControlUnit->OnSendMessage += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnSendMessage);
			this->simulatorBaseControlUnit->OnCputWLCD += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnCputWLCD);
			this->simulatorBaseControlUnit->OnStopSound += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnStopSound);
			this->simulatorBaseControlUnit->OnCputSLCD += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnCputSLCD);
			this->simulatorBaseControlUnit->OnOrderMotor += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnOrderMotor);
			this->simulatorBaseControlUnit->OnCputCLCD += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnCputCLCD);
			this->simulatorBaseControlUnit->OnSetLightBrightness += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnSetLightBrightness);
			this->simulatorBaseControlUnit->OnPlay += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnPlay);
			this->simulatorBaseControlUnit->OnBeepSound += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnBeepSound);
			this->simulatorBaseControlUnit->OnClearMessage += gcnew System::EventHandler(this, &Form1::simulatorBaseControlUnit_OnClearMessage);
			// 
			// label8
			// 
			this->label8->AutoSize = true;
			this->label8->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label8->Location = System::Drawing::Point(9, 472);
			this->label8->Name = L"label8";
			this->label8->Size = System::Drawing::Size(130, 16);
			this->label8->TabIndex = 26;
			this->label8->Text = L"Sound Duration:";
			// 
			// label9
			// 
			this->label9->AutoSize = true;
			this->label9->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label9->Location = System::Drawing::Point(9, 497);
			this->label9->Name = L"label9";
			this->label9->Size = System::Drawing::Size(136, 16);
			this->label9->TabIndex = 27;
			this->label9->Text = L"Sound Internote:";
			// 
			// textBoxSoundDuration
			// 
			this->textBoxSoundDuration->Enabled = false;
			this->textBoxSoundDuration->Location = System::Drawing::Point(144, 473);
			this->textBoxSoundDuration->Name = L"textBoxSoundDuration";
			this->textBoxSoundDuration->Size = System::Drawing::Size(100, 19);
			this->textBoxSoundDuration->TabIndex = 28;
			// 
			// textBoxSoundInternote
			// 
			this->textBoxSoundInternote->Enabled = false;
			this->textBoxSoundInternote->Location = System::Drawing::Point(144, 498);
			this->textBoxSoundInternote->Name = L"textBoxSoundInternote";
			this->textBoxSoundInternote->Size = System::Drawing::Size(100, 19);
			this->textBoxSoundInternote->TabIndex = 29;
			// 
			// label10
			// 
			this->label10->AutoSize = true;
			this->label10->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label10->Location = System::Drawing::Point(250, 455);
			this->label10->Name = L"label10";
			this->label10->Size = System::Drawing::Size(195, 16);
			this->label10->TabIndex = 30;
			this->label10->Text = L"Light Sensor Brightness:";
			// 
			// textBoxLightSensorBrightness
			// 
			this->textBoxLightSensorBrightness->Enabled = false;
			this->textBoxLightSensorBrightness->Location = System::Drawing::Point(451, 456);
			this->textBoxLightSensorBrightness->Name = L"textBoxLightSensorBrightness";
			this->textBoxLightSensorBrightness->Size = System::Drawing::Size(108, 19);
			this->textBoxLightSensorBrightness->TabIndex = 31;
			// 
			// textBoxWheelMotorSpeed
			// 
			this->textBoxWheelMotorSpeed->Enabled = false;
			this->textBoxWheelMotorSpeed->Location = System::Drawing::Point(151, 398);
			this->textBoxWheelMotorSpeed->Name = L"textBoxWheelMotorSpeed";
			this->textBoxWheelMotorSpeed->Size = System::Drawing::Size(93, 19);
			this->textBoxWheelMotorSpeed->TabIndex = 32;
			// 
			// textBoxSteeringMotorSpeed
			// 
			this->textBoxSteeringMotorSpeed->Enabled = false;
			this->textBoxSteeringMotorSpeed->Location = System::Drawing::Point(151, 423);
			this->textBoxSteeringMotorSpeed->Name = L"textBoxSteeringMotorSpeed";
			this->textBoxSteeringMotorSpeed->Size = System::Drawing::Size(93, 19);
			this->textBoxSteeringMotorSpeed->TabIndex = 33;
			// 
			// buttonBattery
			// 
			this->buttonBattery->Location = System::Drawing::Point(169, 365);
			this->buttonBattery->Name = L"buttonBattery";
			this->buttonBattery->Size = System::Drawing::Size(75, 23);
			this->buttonBattery->TabIndex = 34;
			this->buttonBattery->Text = L"update";
			this->buttonBattery->UseVisualStyleBackColor = true;
			this->buttonBattery->Click += gcnew System::EventHandler(this, &Form1::buttonBattery_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(571, 522);
			this->Controls->Add(this->buttonBattery);
			this->Controls->Add(this->textBoxSteeringMotorSpeed);
			this->Controls->Add(this->textBoxWheelMotorSpeed);
			this->Controls->Add(this->textBoxLightSensorBrightness);
			this->Controls->Add(this->label10);
			this->Controls->Add(this->textBoxSoundInternote);
			this->Controls->Add(this->textBoxSoundDuration);
			this->Controls->Add(this->label9);
			this->Controls->Add(this->label8);
			this->Controls->Add(this->buttonExecuteContinue);
			this->Controls->Add(this->checkBoxStepExecute);
			this->Controls->Add(this->buttonClearLightSensorValue);
			this->Controls->Add(this->buttonAddLightSensorValue);
			this->Controls->Add(this->textBoxLightSensorValue);
			this->Controls->Add(this->listBoxSensors);
			this->Controls->Add(this->textBoxSound);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->checkBox1);
			this->Controls->Add(this->textBoxSteeringMotorDir);
			this->Controls->Add(this->textBoxWheelMotorDir);
			this->Controls->Add(this->textBoxBatteryLevel);
			this->Controls->Add(this->label7);
			this->Controls->Add(this->label6);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->textBoxAppName);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->buttonStart);
			this->Controls->Add(this->textBoxRobotOrder);
			this->Controls->Add(this->panel1);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->simulatorBaseControlUnit);
			this->Name = L"Form1";
			this->Text = L"Robot Emulator Consol";
			this->panel1->ResumeLayout(false);
			this->panel1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		RobotApp^ robotApp;
		IRobot* robot;
		ISimulatorControl* simCon;
		UInt32 currentLoopCoun;

		EventWaitHandle^ waitHandle;

	private: System::Void buttonStart_Click(System::Object^  sender, System::EventArgs^  e) {
				 this->currentLoopCoun=0;				 

				 ThreadStart^ ts = gcnew ThreadStart(robotApp,&RobotApp::ExecuteRobot);
				 Thread^ t = gcnew Thread(ts);
				 t->Start();
//				 robotApp->Execute();
			 }

		 delegate System::Void AddTextBoxCallback(TextBox^ tb, String^ statement, Boolean crlf);

		 private: System::Void AddTextBox(TextBox^ tb, String^ statement, Boolean crlf)
				  {
					  if (tb->InvokeRequired) {
						  AddTextBoxCallback^ d = gcnew AddTextBoxCallback(this,&Form1::AddTextBox);
						  array<Object^> ^myArray = {tb,statement,crlf};

						  this->Invoke(d, myArray);
					  }
					  else {
						  if (crlf) {
							  StringBuilder^ sb = gcnew StringBuilder(tb->Text);
							  TextWriter^ writer = gcnew StringWriter(sb);
							  writer->WriteLine(statement);
							  tb->Text=writer->ToString();
						  } else {
							  tb->Text += statement;
						  }
					  }
				  }

		 delegate System::Void SetTextBoxCallback(TextBox^ tb, String^ statement);

		 private: System::Void SetTextBox(TextBox^ tb, String^ statement)
				  {
					  if (tb->InvokeRequired) {
						  SetTextBoxCallback^ d = gcnew SetTextBoxCallback(this,&Form1::SetTextBox);
						  array<Object^> ^myArray = {tb,statement};

						  this->Invoke(d, myArray);
					  }
					  else {
						  tb->Text = statement;
					  }
				  }


				  void RCXKeyChanged(Button^ button){
					  if (button->ForeColor==Color::Black) {
						  button->ForeColor=Color::Red;
					  } else {
						  button->ForeColor = Color::Black;
					  }

					  this->simulatorBaseControlUnit->NotifyKeyStatus(
						  this->buttonOnOff->ForeColor==Color::Red,
						  this->buttonRun->ForeColor==Color::Red,
						  this->buttonView->ForeColor==Color::Red,
						  this->buttonProgram->ForeColor==Color::Red);
				  }

private: System::Void simulatorBaseControlUnit_OnBeepSound(System::Object^ object, System::EventArgs^ e) {
			 this->SetTextBox(this->textBoxSound, "Beep!");
		 }
private: System::Void simulatorBaseControlUnit_OnClearLCD(System::Object^ sender, System::EventArgs^ e) {
			 this->SetTextBox(this->textBoxRCXLCD,"");
		 }
private: System::Void simulatorBaseControlUnit_OnClearMessage(System::Object^ object, System::EventArgs^ e) {
			 ;
		 }
private: System::Void simulatorBaseControlUnit_OnCputCLCD(System::Object^ sender, System::EventArgs^ e) {
			 SimulatorBase::EventArgsCputCLCD^ args = (SimulatorBase::EventArgsCputCLCD^)e;
			 StringBuilder^ sb = gcnew StringBuilder( "");
			 for (int i=0;i<args->Pos;i++) {
				 sb->Append(" ");
			 }
			 sb->Append(args->C);
			 this->SetTextBox(this->textBoxRCXLCD, sb->ToString());
		 }
private: System::Void simulatorBaseControlUnit_OnPlay(System::Object^  sender, System::EventArgs^  e) {
			 SimulatorBase::EventArgsPlay^ args = (SimulatorBase::EventArgsPlay^)e;
			 this->SetTextBox(this->textBoxSound,"Play!"+"Pitch:"+args->Pitch+",Length:"+args->Length);
		 }
private: System::Void buttonView_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->RCXKeyChanged(buttonView);
		 }
private: System::Void buttonProgram_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->RCXKeyChanged(buttonProgram);
		 }
private: System::Void buttonOnOff_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->RCXKeyChanged(buttonOnOff);
		 }
private: System::Void buttonRun_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->RCXKeyChanged(buttonOnOff);
		 }
		 delegate System::Int32 GetSelectedListIndexCallback(ListBox^ lb);
		 System::Int32 GetSelectedListIndex(ListBox^ lb) {
			 Int32 result;
			 if (lb->InvokeRequired) {
				 GetSelectedListIndexCallback^ d = gcnew GetSelectedListIndexCallback(this,&Form1::GetSelectedListIndex);
				 array<Object^>^ myArray = {lb};
				 result = (Int32)this->Invoke(d,myArray);
			 } else {
				 result = lb->SelectedIndex;
			 }

			 return result;
		 }
		 delegate System::Int32 GetListItemsNumCallback(ListBox^ lb);
		 System::Int32 GetListItemsNum(ListBox^ lb) {
			 Int32 result;
			 if (lb->InvokeRequired) {
				 GetListItemsNumCallback^ d = gcnew GetListItemsNumCallback(this,&Form1::GetListItemsNum);
				 array<Object^>^ myArray = {lb};
				 result = (Int32)this->Invoke(d,myArray);
			 } else {
				 result = lb->Items->Count;
			 }

			 return result;
		 }
		 delegate System::Void SetListSelectedItemCallback(ListBox^ lb, Int32 index);
		 System::Void SetListSelectedItem(ListBox^ lb, Int32 index) {
			 if (lb->InvokeRequired) {
				 SetListSelectedItemCallback^ d = gcnew SetListSelectedItemCallback(this,&Form1::SetListSelectedItem);
				 array<Object^>^ myArray = {lb, index};
				 this->Invoke(d,myArray);
			 } else {
				 lb->SelectedIndex = index;
			 }
		 }
		 delegate System::Int32 GetSelectedListIntCallback(ListBox^ lb, Int32 index);
		 System::Int32 GetSelectedListInt(ListBox^ lb, Int32 index) {
			 Int32 result;
			 if (lb->InvokeRequired) {
				 GetSelectedListIntCallback^ d = gcnew GetSelectedListIntCallback(this,&Form1::GetSelectedListInt);
				 array<Object^>^ myArray = {lb,index};
				 result = (Int32)this->Invoke(d,myArray);
			 } else {
				 String^ val  = (String^)(lb->Items[index]);
				 result = Int32::Parse(val);
			 }

			 return result;
		 }

private: System::Void simulatorBaseControlUnit_OnNotifyLoopCount(System::Object^  sender, System::EventArgs^  e) {
			 SimulatorBase::EventArgsNotifyLoopCount^ args = (SimulatorBase::EventArgsNotifyLoopCount^)e;
			 this->AddTextBox(this->textBoxRobotOrder, "LoopCount:"+args->Count,true);

			 int no = this->GetListItemsNum(this->listBoxSensors);
			 int index = this->GetSelectedListIndex(this->listBoxSensors);
			 int value = 0;
			 if (index==-1) {
				 if (no>0){
					 this->SetListSelectedItem(this->listBoxSensors, 0);
				 } else {
					 System::Windows::Forms::MessageBox::Show("Please specify sensor value! current value will be 0!");
				 }
			 } else {
				 value = this->GetSelectedListInt(this->listBoxSensors,index);
			 }
			 int sensorId = this->simulatorBaseControlUnit->LightSensorPort;
			 if (sensorId==0) {
				 this->simulatorBaseControlUnit->SensorS1 = value;
			 } else if (sensorId==1) {
				 this->simulatorBaseControlUnit->SensorS2 = value;
			 } else if (sensorId==2) {
				 this->simulatorBaseControlUnit->SensorS3 = value;
			 }
			 this->AddTextBox(this->textBoxRobotOrder, "Specify:LightSensor,Value:"+value.ToString(),true);

			 index++;
			 if (index>=no) {
				 index = 0;
			 }
			 this->SetListSelectedItem(this->listBoxSensors, index);

			 if (this->checkBoxStepExecute->Checked) {
				 this->waitHandle->WaitOne();
			 }
		 }
				  
private: System::Void buttonAddLightSensorValue_Click(System::Object^  sender, System::EventArgs^  e) {
			 if (this->listBoxSensors->SelectedIndex>=0) {
				 this->listBoxSensors->Items->Insert(this->listBoxSensors->SelectedIndex,this->textBoxLightSensorValue->Text);
			 } 
			 else {
				this->listBoxSensors->Items->Add(this->textBoxLightSensorValue->Text);
			}
		 }
private: System::Void buttonClearLightSensorValue_Click(System::Object^  sender, System::EventArgs^  e) {
			 if (this->listBoxSensors->SelectedIndex>=0) {
				 this->listBoxSensors->Items->RemoveAt(this->listBoxSensors->SelectedIndex);
			 }
		 }
private: System::Void simulatorBaseControlUnit_OnCputSLCD(System::Object^  sender, System::EventArgs^  e) {
			 SimulatorBase::EventArgsCputSLCD^ args = (SimulatorBase::EventArgsCputSLCD^)e;
			 this->SetTextBox(this->textBoxRCXLCD, args->S);
		 }
private: System::Void simulatorBaseControlUnit_OnCputWLCD(System::Object^  sender, System::EventArgs^  e) {
			 SimulatorBase::EventArgsCputWLCD^ args = (SimulatorBase::EventArgsCputWLCD^)e;
			 this->SetTextBox(this->textBoxRCXLCD,args->Word.ToString());
		 }
private: System::Void simulatorBaseControlUnit_OnOrderMotor(System::Object^  sender, System::EventArgs^  e) {
			 String^ speedParam = "";
			 String^ dirParam = "";
			 SimulatorBase::EventArgsOrderMotor^ args = (SimulatorBase::EventArgsOrderMotor^)e;
			 String^ params = args->Param.ToString();
			 if (args->MethodName->CompareTo("brake")==0) {
				 if (args->Param==-1) {
					 params = "";
				 } else {
					 params = "duration:"+params;
					 speedParam = params;
				 }
				 dirParam = "brake";
			 } else if (args->MethodName->CompareTo("brake")==0) {
			 } else if (args->MethodName->CompareTo("direction")==0) {
				 String^ dir="";
				 if (args->Param==0) {
					 dir = "off";
				 } else if (args->Param==1) {
					 dir = "forward";
				 } else if (args->Param==2) {
					 dir = "reverse";
				 } else if (args->Param==3) {
					 dir = "brake";
				 } 
				 dirParam = dir;
				 params = "dir:"+dir;
			 } else if (args->MethodName->CompareTo("forward")==0) {
				 if (args->Param==-1) {
					 params = "";
				 } else {
					 params = "speed:"+params;
					 speedParam = params;
				 }
				 dirParam = "forward";
			 } else if (args->MethodName->CompareTo("off")==0) {
				 params = "";
				 dirParam = "off";
			 } else if (args->MethodName->CompareTo("reverse")==0) {
				 if (args->Param==-1) {
					 params = "";
				 } else {
					 params = "speed:"+params;
					 speedParam = params;
				 }
				 dirParam = "reverse";
			 } else if (args->MethodName->CompareTo("speed")==0) {
				 params = "speed:"+params;
				 speedParam = params;
			 }
			 this->AddTextBox(this->textBoxRobotOrder, "Port:"+args->Port+",Order:"+args->MethodName+params,true);
			 if (this->simulatorBaseControlUnit->WheelMotorPort==(args->Port-1)) {
				 if (speedParam->CompareTo("")!=0) {
					 this->SetTextBox(this->textBoxWheelMotorSpeed,speedParam);
				 }
				 if (dirParam->CompareTo("")!=0){
					 this->SetTextBox(this->textBoxWheelMotorDir, dirParam);
				 }
			 } else if (this->simulatorBaseControlUnit->SteeringMotorPort==(args->Port-1)){
				 if (speedParam->CompareTo("")!=0) {
					 this->SetTextBox(this->textBoxSteeringMotorSpeed, speedParam);
				 }
				 if (dirParam->CompareTo("")!=0) {
					 this->SetTextBox(this->textBoxSteeringMotorDir,dirParam);
				 }
			 }
		 }
private: System::Void simulatorBaseControlUnit_OnSendMessage(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void simulatorBaseControlUnit_OnStopSound(System::Object^  sender, System::EventArgs^  e) {
			 this->SetTextBox(this->textBoxSound,"Stop");
		 }
private: System::Void simulatorBaseControlUnit_OnSetLightBrightness(System::Object^  sender, System::EventArgs^  e) {
			 SimulatorBase::EventArgsSetLightBrightness^ args = (SimulatorBase::EventArgsSetLightBrightness^)e;
			 this->SetTextBox(this->textBoxLightSensorBrightness,args->Level.ToString());
		 }
private: System::Void simulatorBaseControlUnit_OnSetSoundDuration(System::Object^  sender, System::EventArgs^  e) {
			 SimulatorBase::EventArgsSetSoundDuration^ args = (SimulatorBase::EventArgsSetSoundDuration^)e;
			 this->SetTextBox(this->textBoxSoundDuration, args->Length.ToString());
		 }
private: System::Void simulatorBaseControlUnit_OnSetSoundInternote(System::Object^  sender, System::EventArgs^  e) {
			 SimulatorBase::EventArgsSetSoundInternote^ args = (SimulatorBase::EventArgsSetSoundInternote^)e;
			 this->SetTextBox(this->textBoxSoundInternote, args->Length.ToString());
		 }
private: System::Void textBoxBatteryLevel_TextChanged(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void checkBox1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			 bool chekced = this->checkBox1->Checked;
			 int value = 0;
			 if (chekced) {
				 value = 1;
			 }
			 int touchSensorPort = this->simulatorBaseControlUnit->TouchSensorPort;
			 if (touchSensorPort==0) {
				 this->simulatorBaseControlUnit->SensorS1 = value;
			 } else if (touchSensorPort==1) {
				 this->simulatorBaseControlUnit->SensorS2 = value;
			 } else if (touchSensorPort==2) {
				 this->simulatorBaseControlUnit->SensorS3 = value;
			 }
			 this->AddTextBox(this->textBoxRobotOrder, "Specify:TouchSensor,Value:"+value,true);
		 }
private: System::Void buttonExecuteContinue_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->waitHandle->Set();
		 }
private: System::Void checkBoxStepExecute_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			 if (this->checkBoxStepExecute->Checked){
				 this->buttonExecuteContinue->Enabled=true;
			 } else {
				 this->buttonExecuteContinue->Enabled=false;
			 }
		 }
private: System::Void buttonBattery_Click(System::Object^  sender, System::EventArgs^  e) {
			 Int32 result;
			 if (Int32::TryParse(this->textBoxBatteryLevel->Text,System::Globalization::NumberStyles::Integer,nullptr,result)) {
				 this->simulatorBaseControlUnit->BatteryLevel = result;

				 this->AddTextBox(this->textBoxRobotOrder,"Specify:Battery,Value:"+result,true);
			 }
		 }
};
}

